package com.symria.client.system;

import com.symria.framework.util.BaseAction;
import com.symria.framework.util.Config;
import com.symria.framework.util.MD5Util;
import com.symria.framework.util.StringUtil;
import com.symria.server.bo.system.SystemService;
import com.symria.server.po.SysUser;
import com.symria.system.constants.ATTSystem;
import com.symria.system.util.SessionHelper;

public class SystemAction extends BaseAction {
    private String userName;
    private String password;
    private String imgCode;
    private String rpassword;
    private String oldPassword;
    private SystemService systemService;
    
    
    
	public String getRpassword() {
		return rpassword;
	}
	public void setRpassword(String rpassword) {
		this.rpassword = rpassword;
	}
	public String getOldPassword() {
		return oldPassword;
	}
	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getImgCode() {
		return imgCode;
	}
	public void setImgCode(String imgCode) {
		this.imgCode = imgCode;
	}
	public SystemService getSystemService() {
		return systemService;
	}
	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}
	public String login(){
		SysUser user = null;
		user = loginValidate();		
		if(!hasFieldErrors()){
			systemService.updateUserForLogin(user);
			return SUCCESS;
		}
		return ERROR;
		
	}
	public SysUser loginValidate(){
		SysUser user = null;
		if(StringUtil.isEmpty(userName)){
			addFieldError("loginError", "请输入您的用户名!");
		}else if(StringUtil.isEmpty(password)){
			addFieldError("loginError", "请输入您的密码!");
		}else if(!StringUtil.equals(imgCode, SessionHelper.get(ATTSystem.SESSION_IMG_CODE, String.class))){
    		addFieldError("loginError", "您的验证码输入不正确!");
    	}else{
			user =  systemService.findUserByName(userName);
			if(null!=user){
				if(!StringUtil.equals(new MD5Util().getMD5ofStr(password), user.getPassword())){
					addFieldError("loginError", "您输入的密码错误!");
				}
			}else{
				addFieldError("loginError", "用户"+userName+"不存在!或者密码错误!");
			}
		}
		return user;
	}
	public String updatePwd(){
		SysUser u = SessionHelper.getSysUser();
		if(!StringUtil.equals(password, rpassword)){
			addFieldError("error", "两次输入的密码不一致!");
		}else if(!StringUtil.equals(new MD5Util().getMD5ofStr(oldPassword), u.getPassword())){
			addFieldError("error", "原密码输入错误!");
		}else{
			systemService.updatePwd(u,password);
		}
		return hasFieldErrors()?ERROR:SUCCESS;
	}
	
	public String configJs(){
		Config.getValue("");
		setAttr("map", Config.map);
		return SUCCESS;
	}
}
